package com.sx.micowell.framework.adapter.binding;

import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;

import androidx.databinding.BindingAdapter;

import com.sx.widget.view.ClearEditText;

import java.util.ArrayList;

public class EditTextBinding {
    //过滤换行空格
    @BindingAdapter(value = {"editFilter"})
    public static void filterEditText(EditText editText, boolean filter) {
        if (editText != null && filter) {
            InputFilter inputFilter = new InputFilter() {
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    if (source.equals(" ") || source.toString().contentEquals("\n")) {
                        return "";
                    } else {
                        return null;
                    }
                }
            };
            InputFilter[] filters = editText.getFilters();
            ArrayList<InputFilter> inputFilters = new ArrayList<>();
            if (filters != null) {
                for (InputFilter filter1 : filters) {
                    inputFilters.add(filter1);
                }
            }
            inputFilters.add(inputFilter);
            InputFilter[] inputFilters1 = new InputFilter[inputFilters.size()];
            for (int i = 0; i < inputFilters.size(); i++) {
                inputFilters1[i] = inputFilters.get(i);
            }
            editText.setFilters(inputFilters1);
        }
    }

    @BindingAdapter(value = {"clearDrawable"})
    public static void bindClearDrawable(ClearEditText clearEditText, Drawable drawable) {
        if (clearEditText != null) {
            clearEditText.setmClearDrawable(drawable);
        }
    }

    @BindingAdapter(value = {"enableInput"})
    public static void editTextInput(EditText editText, boolean enableInput) {
        if (editText != null) {
            editText.setFocusableInTouchMode(enableInput);//不可编辑
            editText.setClickable(enableInput);//不可点击，但是这个效果我这边没体现出来，不知道怎没用
            editText.setFocusable(enableInput);//不可编辑
        }
    }


}
